﻿Imports System.Web.Configuration
Imports System.Data.SqlClient
Imports System.Data

Partial Class QuizSelect
    Inherits System.Web.UI.Page
    Private connectionString As String = WebConfigurationManager.ConnectionStrings("Database").ConnectionString

    Protected Sub QuizSelect_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            fillQuizListBox(sender, e)
            userNameHiddenField.Value = CType(PreviousPage.FindControl("userNameHiddenField"), HiddenField).Value.ToString
            namelabel.Text = userNameHiddenField.Value.ToString
        End If
    End Sub

    Private Sub fillQuizListBox(sender As Object, e As EventArgs)
        quizListBox.Items.Clear()
        Dim selectSQL As String = "SELECT * FROM QuestionMain"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(selectSQL, myConnection)
        Dim myAdapter As New SqlDataAdapter(myCommand)
        Dim quizDataset As New DataSet()

        Try
            myConnection.Open()
            myAdapter.Fill(quizDataset, "QuestionMain")
            For Each row As DataRow In quizDataset.Tables("QuestionMain").Rows
                Dim newItem As New ListItem
                newItem.Text = row("quizTitle")
                newItem.Value = row("Quiz_Id").ToString
                quizListBox.Items.Add(newItem)
            Next
            quizListBox.SelectedIndex = 0
        Catch ex As Exception
            errorLabel.Text = "Error getting your created quizzes. " & ex.Message

        Finally
            myConnection.Close()
        End Try
    End Sub


    Protected Sub takeQuizButton_Click(sender As Object, e As EventArgs) Handles takeQuizButton.Click
        quizIDHiddenField.Value = quizListBox.SelectedValue
        titleHiddenField.Value = quizListBox.SelectedItem.ToString
        ownerNameHiddenField.Value = creatorLabel.Text
    End Sub

    Protected Sub quizListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles quizListBox.SelectedIndexChanged
        errorLabel.Text = ""
        getOwner(sender, e)
        Dim SelectSQL As String = "SELECT * FROM userTable WHERE User_Id = '" & ownerIDHiddenField.Value & "'"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(SelectSQL, myConnection)
        Dim myReader As SqlDataReader
        Try
            myConnection.Open()
            myReader = myCommand.ExecuteReader
            myReader.Read()
            creatorLabel.Text = myReader("UserName")
        Catch ex As Exception
            errorLabel.Text = "Cant get User name" & ex.Message
        Finally
            myConnection.Close()
        End Try
    End Sub

    Protected Sub getOwner(sender As Object, e As EventArgs)
        Dim selectSQL As String = "SELECT * FROM QuestionMain WHERE Quiz_Id = '" & quizListBox.SelectedValue & "'"
        Dim myConnection As New SqlConnection(connectionString)
        Dim myCommand As New SqlCommand(selectSQL, myConnection)
        Dim myReader As SqlDataReader

        Try
            myConnection.Open()
            myReader = myCommand.ExecuteReader
            myReader.Read()
            ownerIDHiddenField.Value = myReader("OwnerID")
        Catch ex As Exception
            errorLabel.Text = "Error" & ex.Message

        Finally
            myConnection.Close()
        End Try
    End Sub
End Class
